package com.ruoyi.develop.goodsSaledoGongxu.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.factory.factoryProduceSaleAll.domain.FactoryProduceSaleAll;
import com.ruoyi.develop.factory.factoryProduceSaleAll.service.IFactoryProduceSaleAllService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSaledoGongxuUser.domain.GoodsSaledoGongxuUser;
import com.ruoyi.develop.goodsSaledoGongxuUser.service.IGoodsSaledoGongxuUserService;
import com.ruoyi.develop.goodsSaledoGongxuUserMoney.service.IGoodsSaledoGongxuUserMoneyService;
import com.ruoyi.develop.lnGongxu.domain.LnGongxu;
import com.ruoyi.develop.lnGongxu.service.ILnGongxuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsSaledoGongxu.domain.GoodsSaledoGongxu;
import com.ruoyi.develop.goodsSaledoGongxu.service.IGoodsSaledoGongxuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单产品工艺工序关联Controller
 * @author qh
 * @date 2024-11-03
 */
@RestController
@RequestMapping("/goodsSaledoGongxu/goodsSaledoGongxu")
public class GoodsSaledoGongxuController extends BaseController {
    @Autowired
    private IGoodsSaledoGongxuService goodsSaledoGongxuService;
    @Autowired
    private IGoodsSaledoGongxuUserMoneyService goodsSaledoGongxuUserMoneyService;
    @Autowired
    private IGoodsSaledoGongxuUserService goodsSaledoGongxuUserService;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private ILnGongxuService lnGongxuService;
    @Autowired
    private IFactoryProduceSaleAllService iFactoryProduceSaleAllService;
    /**
     * 查询订单产品工艺工序关联列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxu:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSaledoGongxu entity) {
        startPage();
        List<GoodsSaledoGongxu> list = goodsSaledoGongxuService.selectList(entity);
        return getDataTable(list);
    }

    @GetMapping("/listSalePro")
    public TableDataInfo listSalePro(GoodsSaledoGongxu entity) {
        startPage();
        List<GoodsSaledoGongxu> list = goodsSaledoGongxuService.listSalePro(entity);
        return getDataTable(list);
    }

    @GetMapping("/listSaleProByPhone/{ids}")
    public TableDataInfo listSaleProByPhone(@PathVariable("ids") String ids) {
        String[] idlist=ids.split(",");
        startPage();
        List<GoodsSaledoGongxu> list = goodsSaledoGongxuService.listSaleProByPhone(idlist);
        return getDataTable(list);
    }

    @GetMapping("/listSaleProFinish")
    public TableDataInfo listSaleProFinish(GoodsSaledoGongxu entity) {
        startPage();
        List<GoodsSaledoGongxu> list = goodsSaledoGongxuService.listSaleProFinish(entity);
        return getDataTable(list);
    }

    @PostMapping("/addGoodsDoGongxu")
    public AjaxResult addGoodsDoGongxu(@RequestBody GoodsSaledoGongxu entity) {
        /*String saleId = request.getParameter("saleId");
        BigDecimal goodsCount = new BigDecimal(request.getParameter("goodsCount"));
        String ids  = request.getParameter("ids");*/
        String saleId = entity.getSaleId();
        GoodsSale goodsSale=goodsSaleService.selectById(saleId);
        if(goodsSale!=null){
            goodsSale.setState(BusinessConstants.GONGXU_STATUS_1);
            goodsSaleService.update(goodsSale);
        }
        BigDecimal goodsCount = entity.getGoodsCount();
        String ids[] = entity.getIds();
        for(String gongxuId:ids){
            LnGongxu lnGongxu=lnGongxuService.selectById(gongxuId);
            if(lnGongxu!=null) {
                GoodsSaledoGongxu goodsSaledoGongxu = new GoodsSaledoGongxu();
                goodsSaledoGongxu.setXuhao(lnGongxu.getIdx());
                goodsSaledoGongxu.setCode(lnGongxu.getCode());
                goodsSaledoGongxu.setName(lnGongxu.getName());
                goodsSaledoGongxu.setGongxuName(lnGongxu.getGoodsName());
                goodsSaledoGongxu.setGongxuModel(lnGongxu.getGoodsModel());
                goodsSaledoGongxu.setRemark(lnGongxu.getRemark());
                goodsSaledoGongxu.setShuoming(lnGongxu.getShuoming());
                goodsSaledoGongxu.setFile(lnGongxu.getFile());
                goodsSaledoGongxu.setPhoto(lnGongxu.getPhoto());
                goodsSaledoGongxu.setSaleId(saleId);
                goodsSaledoGongxu.setGoodsId(lnGongxu.getGoodsId());
                goodsSaledoGongxu.setMoney(lnGongxu.getMoney());
                goodsSaledoGongxu.setDeptId(lnGongxu.getDeptId());
                goodsSaledoGongxu.setDeptName(lnGongxu.getDeptName());
                goodsSaledoGongxu.setWorkHour(lnGongxu.getWorkHour());
                if(goodsCount!=null){
                    goodsSaledoGongxu.setCounts(goodsCount);
                    goodsSaledoGongxu.setRemainCount(goodsCount);
                    goodsSaledoGongxu.setRemainfinishCount(goodsCount);
                }
                goodsSaledoGongxuService.insert(goodsSaledoGongxu);
            }
        }
        return success();
    }
    /**
     * 导出订单产品工艺工序关联列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxu:export')")
    @Log(title = "订单产品工艺工序关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsSaledoGongxu entity) {
        List<GoodsSaledoGongxu> list = goodsSaledoGongxuService.selectList(entity);
        ExcelUtil<GoodsSaledoGongxu> util = new ExcelUtil<GoodsSaledoGongxu>(GoodsSaledoGongxu. class);
        util.exportExcel(response, list, "订单产品工艺工序关联数据");
    }

    /**
     * 获取订单产品工艺工序关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsSaledoGongxuService.selectById(id));
    }

    /**
     * 新增订单产品工艺工序关联
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxu:add')")
    @Log(title = "订单产品工艺工序关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsSaledoGongxu entity) {
        return toAjax(goodsSaledoGongxuService.insert(entity));
    }

    /**
     * 修改订单产品工艺工序关联
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxu:edit')")
    @Log(title = "订单产品工艺工序关联", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsSaledoGongxu entity) {
        return toAjax(goodsSaledoGongxuService.update(entity));
    }

    /**
     * 删除订单产品工艺工序关联
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxu:remove')")
    @Log(title = "订单产品工艺工序关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsSaledoGongxuService.deleteByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('develop:GoodsSaledoGongxu:remove')")
    @Log(title = "订单产品工艺工序关联删除工序", businessType = BusinessType.DELETE)

    @DeleteMapping("/remove/{ids}")
    public AjaxResult removeGoodsGongxu(@PathVariable String[] ids) {
        GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(ids[0]);
        GoodsSaledoGongxuUser goodsSaledoGongxuUser=new GoodsSaledoGongxuUser();
        goodsSaledoGongxuUser.setSaleId(goodsSaledoGongxu.getSaleId());
        goodsSaledoGongxuUser.setParentId(goodsSaledoGongxu.getId());
        List<GoodsSaledoGongxuUser> goodsSaledoGongxuUsers=goodsSaledoGongxuUserService.selectList(goodsSaledoGongxuUser);
        if(goodsSaledoGongxuUsers.size()>0){
            return error("已分配工序不可修改或删除，请先删除分配的任务再删除！");
        }
        String saleId ="";
        if(goodsSaledoGongxu!=null) {
            saleId =goodsSaledoGongxu.getSaleId();
        }
        goodsSaledoGongxuService.deleteByIds(ids);
        if(StringUtils.isNotBlank(saleId)){
            List<GoodsSaledoGongxu> goodsSaledoGongxus=goodsSaledoGongxuService.selectBySaleId(saleId);
            if(goodsSaledoGongxus.size()>0){

            }else {
                GoodsSale goodsSale = goodsSaleService.selectById(saleId);
                if (goodsSale != null) {
                    goodsSale.setState(BusinessConstants.GONGXU_STATUS_0);
                    goodsSaleService.update(goodsSale);
                }
            }
        }
        return success();

    }
    /**
     * 修改工艺工序关联
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongyigongxu:edit')")
    @Log(title = "批量修改工艺工序图纸号", businessType = BusinessType.UPDATE)
    @PutMapping("/editAllTuzhihao")
    public AjaxResult editAllTuzhihao(@RequestBody GoodsSaledoGongxu entity) {
        return toAjax(goodsSaledoGongxuService.editAllTuzhihao(entity));
    }

    @Log(title = "设定工序的设为最后一道工序", businessType = BusinessType.DELETE)
    @GetMapping("/setLast")
    public AjaxResult setLast(HttpServletRequest request) {
        String id  = request.getParameter("id");
        String saleId  = request.getParameter("saleId");
        goodsSaledoGongxuService.updateAllNotLast(saleId);
        goodsSaledoGongxuService.updateLastById(id);
        //goodsSaledoGongxuUserService.updateAllNotLast(gongyiId);
        //goodsSaledoGongxuUserService.updateLastById(id);
        //goodsSaledoGongxuUserMoneyService.updateAllNotLast(gongyiId);
        //goodsSaledoGongxuUserMoneyService.updateLastById(id);

        GoodsSaledoGongxu goodsSaledoGongxu=goodsSaledoGongxuService.selectById(id);
        if(goodsSaledoGongxu!=null){
            GoodsSaledoGongxu goodsSaledoGongxuLast= goodsSaledoGongxuService.selectLastFinish(goodsSaledoGongxu.getSaleId());
            if(goodsSaledoGongxuLast!=null){
                if(goodsSaledoGongxuLast.getFinishCount()!=null) {
                    if (goodsSaledoGongxuLast.getFinishCount().compareTo(BigDecimal.ZERO) > 0) {
                        FactoryProduceSaleAll factoryProduceSaleAll = iFactoryProduceSaleAllService.selectById(goodsSaledoGongxu.getSaleId());
                        factoryProduceSaleAll.setFinishdoCount(goodsSaledoGongxuLast.getFinishCount());
                        factoryProduceSaleAll.setKerukuCount(goodsSaledoGongxuLast.getFinishCount().subtract(factoryProduceSaleAll.getRukuCount()));
                        iFactoryProduceSaleAllService.update(factoryProduceSaleAll);
                    }
                }
            }
        }

        return  AjaxResult.success();
    }
}
